# This script is used to find out how heavy a given machine thinks
# that a couple of tasks are.

from __future__ import with_statement
from time import time
import sys

def sharpen(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    factor = 1.0 + float(factor)
    sharpened_image = ImageEnhance.Sharpness(pil_image).enhance(factor)
    sio = StringIO()
    sharpened_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

def brightness(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    sharpened_image = ImageEnhance.Brightness(pil_image).enhance(factor)
    sio = StringIO()
    sharpened_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

def color(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    sharpened_image = ImageEnhance.Color(pil_image).enhance(factor)
    sio = StringIO()
    sharpened_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()

def contrast(image, factor):
    from PIL import Image, ImageEnhance
    from StringIO import StringIO
    sio = StringIO(image)
    pil_image = Image.open(sio)
    sharpened_image = ImageEnhance.Contrast(pil_image).enhance(factor)
    sio = StringIO()
    sharpened_image.save(sio, pil_image.format, quality=95)
    return sio.getvalue()


verbose = False
if '-d' in sys.argv:
    verbose = True

strength =  float(sys.argv[1])

if verbose: print 'measuring sharpen'
with open(sys.argv[2]) as infile:
    image = infile.read()
start = time()
sharpen(image, 0.75)
stop = time()
brightness(image, 0.75)
full_stop = time()
if verbose: 
    print 'sharpen time =', stop-start, ' cost =', strength*(stop-start)
    print 'brightness time =', full_stop-stop, ' cost =', strength*(full_stop-stop)
else: 
    print strength, strength*(stop-start), strength*(full_stop-stop)
